<?php

namespace app\common\model;
use app\common\RootModel;

class CategoryModel extends RootModel
{
    protected $pk = 'id';
    protected $table = 'sys_category';

    /**
     * 栏目状态
     * @var string[]
     */
    public static $stateLabel = ['启用', '禁用'];

    /**
     * 栏目类型
     * @var string[]
     */
    public static $typeLabel = ['文章视频', '政策协议', '帮助中心'];


    /**
     * 帮助中心
     * @return \think\model\relation\HasMany
     */
    public function helpList()
    {
        return $this->hasMany(ContentModel::class, 'cat_id', 'id')
            ->join(['sys_content_body' => 'cb'], 'sys_content.id = cb.content_id', 'left')
            ->where(['state' => 1, 'type' => 2])->order(['sort' => 'asc'])->field(['cat_id', 'title', 'cb.content']);
    }

    /**
     * 获取任务
     * @param $val
     * @return mixed
     */
    protected function getTaskAttr($val)
    {
        return json_decode($val, true);
    }

    /**
     * 设置任务
     * @param $val
     * @return false|string
     */
    protected function setTaskAttr($val) {
        return json_encode($val);
    }
}